const app = getApp();
Page({
  data: {
    toggle: false,
    loginHistoryRoles: [],
    actions: [{
      name: '删除',
      color: '#fff',
      fontsize: '22',
      width: 60,
      //icon: 'like.png',//此处为图片地址
      background: '#ed3f14'
    }]
  },
  onLoad: function (options) {
    let loginHistoryRoles = wx.getStorageSync('loginHistoryRoles') || [];
    if (loginHistoryRoles.length > 0) {
      let index = loginHistoryRoles.findIndex(item => item.token == wx.getStorageSync('token'));
      if (~index) {
        loginHistoryRoles[index].checked = true;
      }
    }
    this.setData({
      loginHistoryRoles
    })
  },


  switchLogin(e) {
    const index = e.currentTarget.dataset.index;
    let item = this.data.loginHistoryRoles[index];
    if (item.checked) {
      app.showToast("无需切换登录");
      return;
    } else {
      wx.showModal({
        title: '提示',
        content: '是否切换到当前选中的角色？',
        success: res => {
          if (res.confirm) {
            wx.showLoading({
              title: '角色切换中',
              mask: true
            })
            wx.setStorageSync('token', item.token);
            let role = item.role;
            wx.setStorageSync('role', role);
            //服务器端返回的角色定义数值 1维修人员  2销售 3合伙人
            if (role == 1) {
              this.getEngineerInfo();
              setTimeout(() => {
                wx.reLaunch({
                  url: '/engineering/pages/index/index',
                  success: res => {
                    wx.hideLoading();
                  }
                })
              }, 1000)
            } else if (role == 2) {
              this.getSalemanInfo();
              setTimeout(() => {
                wx.reLaunch({
                  url: '/promoter/pages/index/index',
                  success: res => {
                    wx.hideLoading();
                  }
                })
              }, 1000)
            } else {
              this.getPartnerInfo();
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/partner/index/index',
                  success: res => {
                    wx.hideLoading();
                  }
                })
              }, 1000)
            }
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        },
        fail: e => {

        }
      })
    }
  },



  setLoginHistoryRoles(userInfo) {
    const loginHistoryRoles = wx.getStorageSync('loginHistoryRoles') || [];
    let role = wx.getStorageSync('role');
    userInfo.role = role //将Storage的角色打放入userInfo
    if (loginHistoryRoles.length == 0) {
      loginHistoryRoles.push(userInfo);
    } else {
      let max = 5; //最多5条历史登录数据
      let index = loginHistoryRoles.findIndex(item => {
        return item.role == role && item.id == userInfo.id
      });
      if (~index) {
        loginHistoryRoles[index] = userInfo;
      } else {
        if (loginHistoryRoles.length < max) {
          loginHistoryRoles.unshift(userInfo);
        } else {
          loginHistoryRoles.pop();
          loginHistoryRoles.unshift(userInfo);
        }
      }
    }
    wx.setStorageSync('loginHistoryRoles', loginHistoryRoles)
  },


  getPartnerInfo() {
    app.ajax({
      url: "Partner/User/getPartnerInfo",
      success: res => {
        if (res.data.code == 200) {
          let origin = res.data.data;
          let userInfo = {
            id: origin.id,
            name: origin.account_name,
            account: origin.account,
            token: wx.getStorageSync('token')
          };
          this.setLoginHistoryRoles(userInfo);
        }
      }
    })
  },


  //获取销售的相关信息
  getSalemanInfo() {
    app.ajax({
      url: 'Salesman/Login/getInfo',
      data: {},
      success: res => {
        if (res.data.code == 1000) {
          wx.setStorageSync('userInfo', res.data.data);
          wx.setStorageSync('salesman_id', res.data.data.id);
          let origin = res.data.data;
          let userInfo = {
            id: origin.id,
            name: origin.full_name,
            account: origin.account,
            token: wx.getStorageSync('token')
          };
          this.setLoginHistoryRoles(userInfo);
        }
      }
    })
  },

  //获取维修员信息
  getEngineerInfo() {
    app.ajax({
      url: 'Manage/User/getManageInfo',
      method: "POST",
      data: {},
      success: res => {
        if (res.data.code == 200) {
          wx.setStorageSync('userInfo', res.data.data);
          let origin = res.data.data;
          let userInfo = {
            id: origin.id,
            name: origin.name,
            account: origin.account,
            token: wx.getStorageSync('token')
          };
          this.setLoginHistoryRoles(userInfo);
        }
      }
    })
  },



  handlerCloseButton(e) {
    let index = e.currentTarget.dataset.index;
    console.log(index, "index")

    const list = this.data.loginHistoryRoles;
    list.splice(index, 1);
    this.setData({
      loginHistoryRoles: list,
      toggle: this.data.toggle ? false : true
    })
    let loginHistoryRoles = wx.getStorageSync('loginHistoryRoles');
    loginHistoryRoles.splice(index, 1);
    wx.setStorageSync('loginHistoryRoles', loginHistoryRoles)
    wx.showToast({
      title: '删除成功',
      icon: "success"
    })
    if (this.data.loginHistoryRoles.length <= 0) {
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    }
  },

  navLogin() {
    wx.reLaunch({
      url: '/pages/login/login',
    })
  },

})